﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Configuration
{
    public partial class FormInstallConfiguration : Form
    {
        //MS defined component code for Outlook 2007.
        const string OUTLOOKCODE = "{0638C49D-BB8B-4CD1-B191-050E8F325736}";
        public FormInstallConfiguration()
        {
            InitializeComponent();
            this.Visible = false;
            PluginController.Install.MessageEvent += new PluginController.MessageEventHandler(Install_MessageEvent);
        }

        void Install_MessageEvent(object sender, PluginController.MessageEventArgs e)
        {
            Output(e.Message);
        }

        private void PerformSetupAndInstall()
        {
            //Check for Outlook...If Outlook exists, then we need to install the Outlook PIA's.
            //If Outlook doesn't exist we can't install Outlook PIA's without getting errors, so to ensure
            //we at least have mshtml we install the VS PIA.
            Output("Starting Install");
            if (PluginController.CheckAppAvailability(OUTLOOKCODE))
            {
                Output("Outlook Found");
                RegisterOutlook(true);
                //Check if the OutlookPIA is already installed.
                if (!PluginController.CheckLibAvailability("Microsoft.Office.Interop.Outlook", "12.0.0.0", "neutral", "71e9bce111e9429c"))
                {
                    Output("Office PIA Not Found");
                    bool result = PluginController.Install.InstallOfficePIA();
                    if (result) Output("Office PIA Installed");
                    else Output("PIA Install Failed");
                }
                else
                {
                    Output("Office PIA Found");
                    //Also, make sure that we have mshtml available.  Install if not.
                    if (!PluginController.CheckLibAvailability("Microsoft.mshtml", "7.0.3300.0", "neutral", "b03f5f7f11d50a3a"))
                    {
                        Output("MSHTML Not Found");
                        bool result = PluginController.Install.InstallPIA();
                        if (result) Output("MSHTML Installed");
                        else Output("MSHTML Install Failed");
                    }
                }
            }
            else
            {
                Output("Outlook Not Found");
                RegisterOutlook(false);
                //Since there's no Outlook, make sure that we have mshtml available.  Install if not.
                if (!PluginController.CheckLibAvailability("Microsoft.mshtml", "7.0.3300.0", "neutral", "b03f5f7f11d50a3a"))
                {
                    Output("MSHTML Not Found");
                    bool result = PluginController.Install.InstallPIA();
                    if (result) Output("MSHTML Installed");
                    else Output("MSHTML Install Failed");
                }
            }
            InstallClickToCall();
        }

        private void RegisterOutlook(bool register)
        {
            //Create menu item
            try
            {
                RegistryKey extKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\ClickToCall");
                extKey.SetValue("Outlook", register.ToString().ToLower());
            }
            catch { }
        }

        private void InstallClickToCall()
        {
            try
            {
                Output("Starting Communicator Click To Call Install");
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.FileName = "msiexec.exe";
                p.StartInfo.Arguments = "/i ClickToCallSetup.msi";
                p.StartInfo.CreateNoWindow = false;
                p.Start();
                p.WaitForExit();
                Output("Installation Success");
            }
            catch (Exception e)
            {
                Output("Installation Failed: " + e.Message);
            }
        }

        private void Output(string message)
        {
            System.IO.StreamWriter sw = new System.IO.StreamWriter("installlog.txt", true);
            sw.WriteLine(message);
            sw.Close();
            sw = null;
            this.tbOutput.Text += "  " + message + "\r\n";
        }

        private void FormInstallConfiguration_Load(object sender, EventArgs e)
        {
            PerformSetupAndInstall();
            Application.Exit();
        }
    }
}
